package com.code.petapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.code.petapp.R;
import com.code.petapp.bean.LostBean;

import java.util.List;

public class HallLostAdapter extends RecyclerView.Adapter<HallLostAdapter.MyViewHolder> {
    private Context context;
    private List<LostBean> list;
    private View inflater;
    private OnItemLClickListener mListener;


    public HallLostAdapter(Context context, List<LostBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.item_hall_lost, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }
    public interface OnItemLClickListener {
        boolean onClick1(View parent, int position);
    }
    // 设置点击事件
    public void setOnItemLClickListener(OnItemLClickListener l) {
        this.mListener = l;
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        LostBean lostBean = list.get(position);
        holder.userName.setText(lostBean.getUserName());
        holder.status.setText(lostBean.getStatus());
        holder.desc.setText(lostBean.getDescr());
        holder.name.setText(lostBean.getName());
        holder.time.setText(lostBean.getTime());
        Glide.with(context)
           .load(lostBean.getAvatar())
                .into(holder.avatar);
        Glide.with(context)
                .load(lostBean.getImg())
                .into(holder.img);
        if(null != mListener) {
            holder.imgPhone.setOnClickListener(v -> mListener.onClick1(holder.imgPhone, position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView userName;
        TextView status;
        TextView desc;
        TextView name;
        TextView time;
        ImageView img;
        ImageView imgPhone;
        ImageView avatar;

        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            userName = itemView.findViewById(R.id.txUserName);
            status = itemView.findViewById(R.id.txStatus);
            desc = itemView.findViewById(R.id.txDesc);
            time=itemView.findViewById(R.id.txTimes);
            name = itemView.findViewById(R.id.txName);
            img = itemView.findViewById(R.id.imgPet);
            imgPhone=itemView.findViewById(R.id.imgPhone);
            avatar=itemView.findViewById(R.id.avatar);
        }
    }
}
